<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class AdminsController extends AdminController{

	public function index(){
		$where = 'a.gid=g.id';
		$where .= I('get.gid') > 0 ? ' and a.gid=' . I('get.gid', 0, 'intval') : '';
		$where .= I('get.name') ? ' and a.name like "%' . I('get.name') . '%"' : '';
		$where .= I('get.tel') ? ' and a.tel like "%' . I('get.tel') . '%"' : '';
		$where .= I('get.status', -1) > -1 ? ' and a.status =' . I('get.status') : '';
		$num_list = M('news')->field('count(*) num,author')->group('author')->select();
		$list = $this->getList('a.id,a.name,a.status,a.tel,a.remark,a.gid,g.name as groupname,a.last_login_time,a.last_login_ip', array('group g', 'admins a'), $where, 'id desc', true);
		foreach($num_list as $v){
			foreach($list as $key => $val){
				if($val['name'] == $v['author']){
					$list[$key]['num'] = $v['num'];
				}
			}
		}
		$this->assign('list', $list);
		$groupList = $this->getList('id,name', 'group','');
		$this->assign('groupList', $groupList);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			$_POST['pwd'] = md5($_POST['name'] . '_NiU_' . $_POST['pwd']);
			$result = $this->updateData($_POST, 'admins');
			$this->returnResult($result);
		}
	}

	public function edit(){
		if(IS_POST){
			unset($_POST['set_status']);
			if(!empty($_POST['pwd'])){
				$_POST['pwd'] = md5($_POST['name'] . '_NiU_' . $_POST['pwd']);
			}else{
				unset($_POST['pwd']);
			}
			$result = $this->updateData($_POST, 'admins',2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'admins', array('id' => I('get.id', 0, 'intval')));
			if($info){
				$data['info'] = $info;
				$data['status'] = 1;
			}else{
				$data['info'] = '数据不存在';
				$data['status'] = 0;
			}
			$this->ajaxReturn($data);

			
		}
	}

	public function del(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'admins');
		$this->returnResult($result);
	}
}